From 162f0185f7d5bb9ac84ebd511921c50e7dbe870d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 18 Mar 2014 17:04:36 +0000 Subject: [PATCH] libxl: Make libxl_exec tolerate foofd<=2 Make passing 0, 1, or 2 as stdinfd, stdoutfd or stderrfd work properly. Also, document the meaning of the fd arguments. Signed-off-by: Ian Jackson CC: Roger Pau Monne CC: Vasiliy Tolstov Acked-by: Ian Campbell --- tools/libxl/libxl_exec.c | 6 +++--- tools/libxl/libxl_internal.h | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/libxl/libxl_exec.c b/tools/libxl/libxl_exec.c index 4b012dc7ad..478b4c22d1 100644 --- a/tools/libxl/libxl_exec.c +++ b/tools/libxl/libxl_exec.c @@ -77,11 +77,11 @@ void libxl__exec(libxl__gc *gc, int stdinfd, int stdoutfd, int stderrfd, if (stderrfd != -1) dup2(stderrfd, STDERR_FILENO); - if (stdinfd != -1) + if (stdinfd > 2) close(stdinfd); - if (stdoutfd != -1 && stdoutfd != stdinfd) + if (stdoutfd > 2 && stdoutfd != stdinfd) close(stdoutfd); - if (stderrfd != -1 && stderrfd != stdinfd && stderrfd != stdoutfd) + if (stderrfd > 2 && stderrfd != stdinfd && stderrfd != stdoutfd) close(stderrfd); check_open_fds(arg0); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 69d2ba8b73..17890ef431 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -1418,6 +1418,11 @@ _hidden int libxl__xenstore_child_wait_deprecated(libxl__gc *gc, * * The last entry of the array always has to be NULL. * + * stdinfd, stdoutfd, stderrfd will be dup2'd onto the corresponding + * fd in the child, if they are not -1. The original copy of the + * descriptor will be closed in the child (unless it's 0, 1 or 2 + * ie the source descriptor is itself stdin, stdout or stderr). + * * Logs errors, never returns. */ _hidden void libxl__exec(libxl__gc *gc, int stdinfd, int stdoutfd, -- 2.30.2